/**************************************************************************/
/*                                                                        */
/*          	Portions Copyright (C)2013, Jason Whelan                  */
/*                                                                        */
/**************************************************************************/
/*                                                                        */
/*                              WWIV Version 5.0x                         */
/*               Copyright (C)2007, WWIV Software Services                */
/*                                                                        */
/*    Licensed  under the  Apache License, Version  2.0 (the "License");  */
/*    you may not use this  file  except in compliance with the License.  */
/*    You may obtain a copy of the License at                             */
/*                                                                        */
/*                http://www.apache.org/licenses/LICENSE-2.0              */
/*                                                                        */
/*    Unless  required  by  applicable  law  or agreed to  in  writing,   */
/*    software  distributed  under  the  License  is  distributed on an   */
/*    "AS IS"  BASIS, WITHOUT  WARRANTIES  OR  CONDITIONS OF ANY  KIND,   */
/*    either  express  or implied.  See  the  License for  the specific   */
/*    language governing permissions and limitations under the License.   */
/*                                                                        */
/**************************************************************************/

#include "TextUI.h"


UIInputBox::UIInputBox( UIView* parent, int height, int width, int starty, int startx,
	std::string prompt, std::string title, char maskCharacter, int len,
	int colorPair, bool erase, bool box ) : 
		UIWindow( parent, height, width, starty, startx, colorPair, erase, box )
{

	m_maskCharacter = maskCharacter;
	m_length = len;
	m_prompt = prompt;
	SetTitle( title );
}

bool UIInputBox::Run()
{

    GetContentPanel()->SetAttribute( UIColors::MENU_SELECTED_COLOR );
    GetContentPanel()->WriteCentered( GetHeight() - 3, " [ OK ] " );
	GetContentPanel()->SetAttribute( UIColors::TEXT_COLOR );
	GetContentPanel()->WriteAt( 1, 0, m_prompt );
	char str[256];

	Paint();	

	echo();
	nocbreak();

	if(mvwgetnstrex(GetContentPanel()->m_peer, 1, 1, str, m_length, m_maskCharacter) !=ERR) {
		SetText(str);
		noecho();
		return true;
	} else {
		noecho();
		return false;
	}


// old hacked code I didn't like, fixed above

	//SetXY(1,1); // set cursor position properly
	//wmove(GetContentPanel()->m_peer, 1, 1);
	//refresh();
	//wrefresh(GetContentPanel()->m_peer);
	
/*	while ( true ) {

		if(m_maskCharacter.length() > 0 && GetText().length() > 0)	{
			GetContentPanel()->WriteAt( 1, 1, m_maskCharacter );
			for(size_t i = 1; i < GetText().length(); i++)	{
				GetContentPanel()->Write( m_maskCharacter );
			}
		} else {
			
			GetContentPanel()->WriteAt( 1, 1, GetText());
		}
	    Paint();
        int key = GetKey();
        switch ( key ) {
			case '\n':
			return true;
			case 0x1b:
			return false;
			case KEY_BACKSPACE:
			case 0x08:
				if (m_text.length()) {
					m_text.resize(m_text.length()-1);
					mvwaddstr(GetContentPanel()->m_peer, 1, m_text.length(), "\b \b");
				}
				//GetContentPanel()->WriteAt( 1, m_text.length()-1, "\0");
			break;
			default:
				if(isalpha(key) || isdigit(key) || isascii(key)) {
						m_text += (char)key ;
				}
        }
    }*/
    return true;
}

